//
//  BKOrderChooseCouponsCell.m
//  bestkeep
//
//  Created by 马乾征 on 16/11/18.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKOrderChooseCouponsCell.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"
/**
    基类
 */
@implementation BKOrderChooseCouponsCell
- (void)fillData:(id)data{}
+(CGFloat)computeHeight:(id)data{
    return 0;
}

@end

/**
    优惠券
 */
@implementation BKOrderCouponsCell
{

    UIImageView *_whiteCard;
    UIImageView *_card;
    UIImageView *_selector;
    UIImageView *_onlySomeDays;

    UILabel *_moneyAmount;
    UILabel *_conditionsOfUse;
    UILabel *_timeLimit;
    
    CAShapeLayer *_roundUp;
    CAShapeLayer *_roundDown;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    
    _whiteCard = [[UIImageView alloc] init];
    _whiteCard.image = [UIImage imageNamed:@"BKOrder_coupon_whiteCard"];
    [self.contentView addSubview:_whiteCard];
    
    _selector = [[UIImageView alloc] init];
    [self.contentView addSubview:_selector];

    _card = [[UIImageView alloc] init];
    [self.contentView addSubview:_card];
    
    _onlySomeDays =[[UIImageView alloc] init];
    [self.contentView addSubview:_onlySomeDays];
    
    _moneyAmount = [[UILabel alloc] init];
    _moneyAmount.textColor = kOrderWhite;
    _moneyAmount.font = UIFontCustom(25);
    [self.contentView addSubview:_moneyAmount];
    
    _conditionsOfUse = [[UILabel alloc] init];
    _conditionsOfUse.textColor = kOrderLightGrey1;
    _conditionsOfUse.font = UIFontCustom(13);
    _conditionsOfUse.numberOfLines = 2;
    [self.contentView addSubview:_conditionsOfUse];
    
    _timeLimit = [[UILabel alloc] init];
    _timeLimit.textColor = kOrderLightGrey1;
    _timeLimit.font = UIFontCustom(10);
    _timeLimit.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:_timeLimit];

    [self makeConstraints];
}

- (void)makeConstraints
{
    [_selector mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView).offset(-5);
        make.left.mas_equalTo(self.contentView).offset(ApplicationBackGauge);
        make.width.height.mas_equalTo(19);
    }];
    
    [_card mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView).offset(-10);
        make.left.mas_equalTo(_selector.mas_right).offset(11);
        make.right.mas_equalTo(_whiteCard.mas_left);
    }];
    
    [_whiteCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView).offset(-10);
        make.right.mas_equalTo(self.contentView).offset(-15);
        make.width.mas_equalTo(_card.mas_width).multipliedBy(1.8);
    }];
    
    [_moneyAmount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(_card);
    }];
    
    [_conditionsOfUse mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_whiteCard).offset(10);
        make.top.mas_equalTo(_whiteCard).offset(10);
        make.right.mas_equalTo(_whiteCard).offset(-5);
    }];
    
    [_timeLimit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_conditionsOfUse);
        make.bottom.mas_equalTo(_whiteCard).offset(-10);
    }];
    
    [_onlySomeDays mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_timeLimit.mas_right);
        make.bottom.mas_equalTo(_timeLimit);
        make.width.mas_equalTo(49.5);
        make.height.mas_equalTo(15);
    }];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    _selector.image = [UIImage imageNamed:selected ? @"BKOrder_selected" : @"BKOrder_unselected"];
}

- (void)fillData:(id)data
{
    BK_orders_dic_order_shoppingTicketList_Model *coupon = data;
    if (coupon)
    {
        _moneyAmount.attributedText = [self generatePriceStr:coupon.reduceCost];

        NSString *conditionStr;
        if (coupon.usedRangeStr && ![coupon.usedRangeStr isEqualToString:@""])
        {
            conditionStr = [NSString stringWithFormat:@"%@\n%@",coupon.usedRangeStr, coupon.useTypeStr];
        }
        else
        {
            conditionStr = coupon.useTypeStr;
        }
        _conditionsOfUse.text = conditionStr;
        
        _timeLimit.text = coupon.invalidTimeStr;
        
        NSArray *imageNameArr = @[@"BKOrder_only_one_days", @"BKOrder_only_two_days", @"BKOrder_only_three_days"];
        if (coupon.remainingWarning && ![coupon.remainingWarning isEqual: @""]) {
            NSInteger numberDay = [[coupon.remainingWarning substringWithRange:NSMakeRange(2, 1)] integerValue];
            _onlySomeDays.image = [UIImage imageNamed:imageNameArr[numberDay - 1]];
            [self limitTimeLabelWidth:coupon.invalidTimeStr];
        }
        _card.image = [coupon.canUseFlag boolValue] ? [UIImage imageNamed:@"BKOrder_coupon_card"] : [UIImage imageNamed:@"BKOrder_coupon_card_gray"];
    }
}

- (void)limitTimeLabelWidth:(NSString *)timeString
{
    CGSize textSize = [timeString sizeWithAttributes:@{NSFontAttributeName : UIFontCustom(10)}];
    CGFloat maxTextWidth = (CGRectGetWidth(self.contentView.frame) - 10 - 10 - 19 - 15) / 2.8 * 1.8 - 10 - 49.5 - 5;
    if (textSize.width > maxTextWidth) {
        [_timeLimit mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(maxTextWidth);
        }];
    }
}

+ (CGFloat)computeHeight:(id)data{
    return 101.5;
}

- (NSMutableAttributedString *)generatePriceStr:(NSString *)priceString{
    NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@",priceString]];
    [finalStr addAttribute:NSFontAttributeName value:UIFontCustom(18) range:NSMakeRange(0, 1)];
    [finalStr addAttribute:NSFontAttributeName value:UIFontCustom(28) range:NSMakeRange(1, priceString.length - 3)];
    [finalStr addAttribute:NSFontAttributeName value:UIFontCustom(15) range:NSMakeRange(finalStr.length - 3, 3)];
    return finalStr;
}

@end


/**
    无购物券cell
 */
@implementation BKOrderCouponsNullCell
{
    UIImageView *_nullImg;
    UILabel     *_msg;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _nullImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BKOrder_nullCoupons"]];
    _nullImg.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_nullImg];
    
    _msg = [[UILabel alloc] init];
    _msg.text = @"暂无可用优惠券";
    _msg.textColor = kOrderLightGrey1;
    _msg.font = UIFontNormal;
    [self.contentView addSubview:_msg];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_nullImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.centerY.mas_equalTo(self.contentView).offset(-32);
    }];
    
    [_msg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(_nullImg.mas_bottom).offset(20);
    }];
}

- (void)fillData:(id)data
{

}

+ (CGFloat)computeHeight:(id)data
{
    return kOrderScreenHeight - 119 - 68;
}

@end
